<%--
  @author Kusal Samarakoon
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>


<script type="text/javascript">

//var associationArray = new Array();
//var associationCount = 0;

var selectedLandList = new Array();
var selectedLandIDArray = new Array();
var landInfoSelected = new Array();


$(function() {
    $('#land-schedule-form').hide();
    initData();
    $('select#gnDivisionId').bind('change', function(evt1) {
        initData();
    });
});


function initData() {
    var id = $('select#gnDivisionId').attr('value');

    var mode = "toBeAssociationLandList";

    var type = "detail";
    $.getJSON('${pageContext.request.contextPath}/common/LandLookupService', {id:id, mode:mode, type:type}, function(data) {
        //loadLandList(data.landList);
        selectedLandList = data.landAssociationDetailsList;
        loadLandListAssociation(data.landAssociationDetailsList);
    });
}


/*function validateLandList() {

    alert($("#landKachcheryList").val());

    $("#schedule-land-visit-plan").validate({
        rules:{
            alienationMethodSetId :{
                required:true
            }
        }

    });
}*/

function validateKachchery() {
    $("#save-kachchery-list").validate({
        rules:{

            kachchery_list_name :{
                required:true
            }
        }
    });
}


function loadLandList() {

    selectedLandIDArray = $("input[name=landSelectedList]:checked").map(
                                                                       function () {
                                                                           return this.value;
                                                                       }).get();


    if(selectedLandIDArray.length < 1){
       alert("Pleace Select at least one land");
        return;
    }

    var landInfo = new Array();
    for (i = 0; i < selectedLandIDArray.length; i++) {
         var res=false;
        for(j = 0; j < landInfoSelected.length; j++){
            if(landInfoSelected[j].id == selectedLandList[selectedLandIDArray[i]].id){
                res=true;
                break;
            }
        }
        if(!res){
           landInfo[landInfo.length] = selectedLandList[selectedLandIDArray[i]];
        }

    }

    landInfoSelected = landInfoSelected.concat(landInfo);
    landInfo = landInfoSelected;
    var opt = '';
    opt += '<thead>' +
            '<tr class="table-header">' +
            '<th class="center-align">' + $('#sel').val() + '<!--<input onclick="checkAllKachchery();" checked title="All" type="checkbox"  id="checkAllIdKachhcery"/>--></th>' +
            '<th class="center-align">' + $('#ref').val() + '</th>' +
            '<th class="left-align">' + $('#lname').val() + '</th>' +
            '<th class="left-align">' + $('#display').val() + '</th>' +
            '<th class="center-align">1&nbsp;</th>' +
            '<th class="center-align">2&nbsp;</th>' +
            '<th class="center-align">3&nbsp;</th>' +
            '<th class="center-align">4&nbsp;</th>' +
            '<th class="center-align">5&nbsp;</th>' +
            '<th class="center-align">6&nbsp;</th>' +
            '<th class="center-align">7&nbsp;</th>' +

            '</tr>' +
            '</thead>';
    if (landInfo.length > 0) {
        opt += '<tbody>';
        for (var i = 0; i < landInfo.length; i++) {


            opt += '<tr>' +
                    '<td class="center-align"><input  checked title="' + $('#sel').val() + '" type="checkbox" name="landKachcheryList" value="' + landInfo[i].id + '"/></td>' +

                    '<td class="center-align">' + landInfo[i].id + '</td>' +
                    '<td>' + landInfo[i].nameOfTheLand + '</td>' +
                    '<td class="center-align"><input type="button" value="View Land" onClick="viewLandDetails(\'' + landInfo[i].id + '\')" /></td>' ;
            if(landInfo[i].alienationMethods != null && landInfo[i].alienationMethods.length >0){

                if( landInfo[i].landPermit==1){
                    opt +=        '<td class="center-align"><input checked title="' + $('#viewTooltipLp').val() + '" type="checkbox" name="amPermitList" value="' + landInfo[i].id + '" /></td>' ;

                }else {
                    opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipLp').val() + '" type="checkbox" name="amPermitList" value="' + landInfo[i].id + '" /></td>' ;

                }


                if( landInfo[i].landGrant==1){
                    opt +=        '<td class="center-align"><input checked title="' + $('#viewTooltipLg').val() + '" type="checkbox" name="amGrantList" value="' + landInfo[i].id + '" /></td>' ;

                }else{
                    opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipLg').val() + '" type="checkbox" name="amGrantList" value="' + landInfo[i].id + '" /></td>' ;

                }


                if( landInfo[i].shortTerm==1){
                    opt +=        '<td class="center-align"><input checked title="' + $('#viewTooltipStl').val() + '" type="checkbox" name="amSTLList" value="' + landInfo[i].id + '" /></td>' ;

                }else{
                    opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipStl').val() + '" type="checkbox" name="amSTLList" value="' + landInfo[i].id + '" /></td>' ;

                }


                if( landInfo[i].longTerm==1){
                    opt +=        '<td class="center-align"><input checked title="' + $('#viewTooltipLtl').val() + '" type="checkbox" name="amLTLList" value="' + landInfo[i].id + '" /></td>' ;

                }else{
                    opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipLtl').val() + '" type="checkbox" name="amLTLList" value="' + landInfo[i].id + '" /></td>' ;

                }


                if( landInfo[i].dep==1){
                    opt +=        '<td class="center-align"><input checked title="' + $('#viewTooltipRd').val() + '" type="checkbox" name="amRDList" value="' + landInfo[i].id + '" /></td>' ;

                }else {
                    opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipRd').val() + '" type="checkbox" name="amRDList" value="' + landInfo[i].id + '" /></td>' ;

                }


                if( landInfo[i].vesting==1){
                    opt +=        '<td class="center-align"><input checked title="' + $('#viewTooltipVo').val() + '" type="checkbox" name="amVOList" value="' + landInfo[i].id + '" /></td>' ;

                }else {
                    opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipVo').val() + '" type="checkbox" name="amVOList" value="' + landInfo[i].id + '" /></td>' ;

                }


                if( landInfo[i].special==1){
                    opt +=        '<td class="center-align"><input checked title="' + $('#viewTooltipSp').val() + '" type="checkbox" name="amSPList" value="' + landInfo[i].id + '" /></td>' ;

                } else {
                    opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipSp').val() + '" type="checkbox" name="amSPList" value="' + landInfo[i].id + '" /></td>' ;

                }



            } else{
                opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipLp').val() + '" type="checkbox" name="amPermitList" value="' + landInfo[i].id + '" /></td>' ;
                opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipLg').val() + '" type="checkbox" name="amGrantList" value="' + landInfo[i].id + '" /></td>' ;
                opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipStl').val() + '" type="checkbox" name="amSTLList" value="' + landInfo[i].id + '" /></td>' ;
                opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipLtl').val() + '" type="checkbox" name="amLTLList" value="' + landInfo[i].id + '" /></td>' ;
                opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipRd').val() + '" type="checkbox" name="amRDList" value="' + landInfo[i].id + '" /></td>' ;
                opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipVo').val() + '" type="checkbox" name="amVOList" value="' + landInfo[i].id + '" /></td>' ;
                opt +=        '<td class="center-align"><input  title="' + $('#viewTooltipSp').val() + '" type="checkbox" name="amSPList" value="' + landInfo[i].id + '" /></td>' ;

            }


            opt +=       '</tr>';
        }
        $('#land-schedule-form').show();
    } else {
        opt += '<tbody>';
        $('#land-schedule-form').hide();
    }
    opt += '</tbody>';
    $('.land-table-schedule').html(opt);
    $('.land-table-schedule').dataTable({
        "sPaginationType": "full_numbers",
        "bDestroy": true,
        "oLanguage": {
            "sZeroRecords": $('#no-record').val(),
            "sLengthMenu": $('#display').val() + " _MENU_ " + $('#records').val(),
            "sSearch": $('#search').val(),
            "sInfo": $('#showing').val() + " _START_ " + $('#to').val() + " _END_ " + $('#of').val() + " _TOTAL_ " + $('#entries').val(),
            "sInfoEmpty": $('#no-record').val(),
            "oPaginate": {
                "sFirst": $('#first').val(),
                "sLast": $('#last').val(),
                "sNext": $('#next').val(),
                "sPrevious": $('#previous').val()
            }
        },
        "fnDrawCallback": function() {
            if ($('.dataTables_paginate span span.paginate_button').size()) {
                $('.dataTables_paginate')[0].style.display = "block";
            } else {
                $('.dataTables_paginate')[0].style.display = "none";
            }
        }
    });
}

function loadLandListAssociation(landInfo) {
    var opt = '';
    opt += '<thead>' +
            '<tr class="table-header">' +
            '<th class="center-align">' + $('#ref').val() + '</th>' +
            '<th class="left-align">' + $('#lname').val() + '</th>' +
            '<th class="left-align">' + $('#display').val() + '</th>' +
            '<th class="center-align">' + $('#sel').val() + '<!--<input onclick="chkall();"  title="All" type="checkbox"  id="checkAllIdLand"/>--></th>' +

            '</tr>' +
            '</thead>';
    if (landInfo.length > 0) {
        opt += '<tbody>';
        for (var i = 0; i < landInfo.length; i++) {


            opt += '<tr>' +

                    '<td class="center-align">' + landInfo[i].id + '</td>' +
                    '<td>' + landInfo[i].nameOfTheLand + '</td>' +
                    '<td class="center-align"><input type="button" value="View Land" onClick="viewLandDetails(\'' + landInfo[i].id + '\')" /></td>' +
                    '<td class="center-align"><input title="' + $('#sel').val() + '" type="checkbox" name="landSelectedList" value="' + i + '" /></td>' ;


            opt +=  '</tr>';
        }
        $('#land-schedule-form').show();
    } else {
        opt += '<tbody>';
        $('#land-schedule-form').hide();
    }
    opt += '</tbody>';
    $('.land-table').html(opt);
    $('.land-table').dataTable({
        "sPaginationType": "full_numbers",
        "bDestroy": true,
        "oLanguage": {
            "sZeroRecords": $('#no-record').val(),
            "sLengthMenu": $('#display').val() + " _MENU_ " + $('#records').val(),
            "sSearch": $('#search').val(),
            "sInfo": $('#showing').val() + " _START_ " + $('#to').val() + " _END_ " + $('#of').val() + " _TOTAL_ " + $('#entries').val(),
            "sInfoEmpty": $('#no-record').val(),
            "oPaginate": {
                "sFirst": $('#first').val(),
                "sLast": $('#last').val(),
                "sNext": $('#next').val(),
                "sPrevious": $('#previous').val()
            }
        },
        "fnDrawCallback": function() {
            if ($('.dataTables_paginate span span.paginate_button').size()) {
                $('.dataTables_paginate')[0].style.display = "block";
            } else {
                $('.dataTables_paginate')[0].style.display = "none";
            }
        }
    });
}


function viewLandDetails(landId) {
    /*var url = '
${"findClildrenByParentLand.do?landId=' + landId + '"}';
     $.get(url, $(this).serialize(), function(data) {
     $("#landDetails").html(data);
     });*/

    var url = '${pageContext.request.contextPath}' + '/viewDetailLandReportByAttachLand.do?landId=' + landId;

    myWindow = window.open(url, '', 'width=980,height=500,scrollbars=yes');
    myWindow.focus();


}

function viewLandAssociation(landId) {

    var url = '${pageContext.request.contextPath}' + '/initLandAssociationReport.do?landId=' + landId;

    myWindow = window.open(url, '', 'width=980,height=500,scrollbars=yes');
    myWindow.focus();

}

/*function chkall(){
    alert(1);
    $('.someclass').attr('checked', this.checked);
}*/

</script>

<div id="land-registration-module-home-title" class="module-home-title">
    <s:a href="landRegistrationModule.do">
        <span><img src="${pageContext.request.contextPath}/images/land-reg.png" alt="Land Registration">
            <s:label value="%{getText('create_land_kachchery.label')}"/></span>
    </s:a>

    <div id="back-button" align="right">
            <s:form><s:submit value="%{getText('back_btn.label')}" action="landRegistrationModule"/></s:form>
        </div>
</div>

<div id="init-land-update-list">
    <div class="page-title">
        <h2><s:label value="%{getText('create_land_kachchery.label')}"/></h2>
    </div>
    <s:if test="hasActionErrors() || hasActionMessages()">
        <div id="message-box" class="message-box">
            <div class="action-msg"><s:actionmessage/></div>
            <div class="action-err"><s:actionerror/></div>
        </div>
    </s:if>
    <s:form id="schedule-land-visit-plan" name="schedule-land-visit-plan"
            method="POST" onsubmit="javascript:return validateFormForInputDataSyntaxErrors('schedule-land-visit-plan')">
        <table class="width-500-px margin-10-auto">
            <tr>
                <td><s:label value="%{getText('district.label')}"/></td>
                <td><s:textfield id="districtId" name="districtName" cssClass="width-200-px" disabled="true"/></td>
            </tr>
            <tr>
                <td><s:label value="%{getText('ds_division.label')}"/></td>
                <td><s:textfield id="dsDivisionId" name="dsDivisionName" cssClass="width-200-px" disabled="true"/></td>
            </tr>
            <tr>
                <td><s:label value="%{getText('gn_division.label')}"/></td>
                <td><s:select id="gnDivisionId" name="gnDivisionId" list="gnDivisionList"
                               headerKey="-1" headerValue="%{getText('all.label')}" cssClass="width-200-px"/></td>
            </tr>
        </table>


        <h3 align="left"><s:label value="%{getText('approved_land_list.label')}"/></h3>
        <%--<table class="width-100 margin-10-auto table land-table">

        </table>--%>

        <table class="width-100 margin-10-auto table land-table">

        </table>


        <fieldset id="land-schedule-form" style="border:none;">


            <div class="form-button-div right-align">
                <%--<s:submit action="addLandKachchery" onclick="validateLandList()"
                          value="%{getText('add_to_kachchery_list.label')}"/>
                <s:submit cssClass="cancel" action="landRegistrationModule" value="%{getText('back.label')}"/>--%>

                <input type="button" value="Add Lands to Kachchery" onclick="loadLandList()">
            </div>
        </fieldset>
    </s:form>
</div>
</br>

<s:form id="save-kachchery-list" name="save-kachchery-list" enctype="multipart/form-data"
        method="POST" onsubmit="javascript:return validateFormForInputDataSyntaxErrors('save-kachchery-list')">


    <div id="view-land-kachchery-pending-list-div">
        <h3 align="left"><s:label value="%{getText('kachchery_land_list.label')}"/></h3>

        <s:include value="alienationMethodList.jsp"/>

        <table class="width-100 margin-10-auto table land-table-schedule">

        </table>


    </div>

    <div id="savekachcheryListDiv">


        <fieldset id="land-schedule-form-id" style="border:none;">
            <table class="width-100">

                <tr>
                    <td><s:label value="%{getText('kachchery_list_name.label')}"
                                 cssClass="mandatory-field width-300-px"/></td>
                    <td><s:textfield id="kachchery_list_name" name="kachchery.name"/></td>
                </tr>

                <tr>
                    <td><s:label value="%{getText('description.label')}"/></td>
                    <td><s:textarea id="discriptionId" name="kachchery.description" cssClass="width-450-px"/></td>
                </tr>
            </table>
            <br/>

            <table class="width-100">
                <s:if test="kachchery.comment.length() !=0">
                    <tr>
                        <td><s:label value="%{getText('comments_history.label')}"/></td>
                        <td><s:textarea id="oldCommentId" name="kachchery.comment" cssClass="width-450-px"
                                        readonly="true"/></td>
                    </tr>
                </s:if>
                <tr>
                    <td><s:label value="%{getText('comment.label')}"/></td>
                    <td><s:textarea id="commentId" name="comment" cssClass="width-595-px"/></td>
                </tr>
            </table>

             <s:include value="../../documentUpload.jsp"/>

            <div class="form-button-div right-align">
                <s:submit action="saveKachchery" onclick="validateKachchery()" value="%{getText('save.label')}"/>
                <s:submit cssClass="cancel" action="landRegistrationModule" value="%{getText('back.label')}"/>


            </div>
        </fieldset>

    </div>

</s:form>


<%-- Localization text for java script generated text --%>
<%--<s:hidden id="ref" value="%{getText('reference_no.label')}"/>--%>
<s:hidden id="ref" value="%{getText('slmsSerialNumber.label')}"/>
<s:hidden id="lname" value="%{getText('name_of_the_land.label')}"/>
<s:hidden id="plan" value="%{getText('plan_no.label')}"/>
<s:hidden id="lot" value="%{getText('lot_no.label')}"/>
<s:hidden id="extent" value="%{getText('extent_perches.label')}"/>
<s:hidden id="select" value="%{getText('select.label')}"/>
<s:hidden id="viewTooltip" value="%{getText('schedule_tooltip')}"/>
<s:hidden id="undifined" value="%{getText('undefined.label')}"/>

<s:hidden id="viewTooltipLp" value="%{getText('land_permits.label')}"/>
<s:hidden id="viewTooltipLg" value="%{getText('land_grants.label')}"/>
<s:hidden id="viewTooltipStl" value="%{getText('short_term_lease.label')}"/>
<s:hidden id="viewTooltipLtl" value="%{getText('long_term_lease.label')}"/>
<s:hidden id="viewTooltipRd" value="%{getText('release_to_departments.label')}"/>
<s:hidden id="viewTooltipVo" value="%{getText('vesting_order.label')}"/>
<s:hidden id="viewTooltipSp" value="%{getText('special_grant.label')}"/>

<%-- Localization text for data tables --%>
<s:hidden id="display" value="%{getText('display.label')}"/>
<s:hidden id="records" value="%{getText('records.label')}"/>
<s:hidden id="search" value="%{getText('search.label')}"/>
<s:hidden id="no-record" value="%{getText('no_record.label')}"/>
<s:hidden id="showing" value="%{getText('showing.label')}"/>
<s:hidden id="to" value="%{getText('to.label')}"/>
<s:hidden id="of" value="%{getText('of.label')}"/>
<s:hidden id="entries" value="%{getText('entries.label')}"/>
<s:hidden id="first" value="%{getText('first.label')}"/>
<s:hidden id="last" value="%{getText('last.label')}"/>
<s:hidden id="next" value="%{getText('next.label')}"/>
<s:hidden id="previous" value="%{getText('previous.label')}"/>
<s:hidden id="pls_enter_task" value="%{getText('pls_enter_task.label')}"/>
<s:hidden id="pls_enter_date" value="%{getText('pls_enter_valid_date.label')}"/>

<s:hidden id="sel" value="%{getText('select_kachchery.label')}"/>
